/**
 * @author bhileman
 * created on 6/28/07
 * manages all the configuration options on the widget configuration utility.
 */
function resetPageStyles()
  {
    //Style the main container
    var elStyle= $("annoration-widget-main-container").style;
    elStyle.border = "solid 1px #2E3192";
    
    //style the header
    elStyle = $("annoration-widget-header").style;
    elStyle.background = "#2E3192";
    elStyle.color = "#fff";
    elStyle.marginTop = "0";
    elStyle.textAlign = "center";
    elStyle.padding = "5px 0px";
    elStyle.fontSize = "110%";
    
    
    //style the main content
    var elStyle = $('annoration-widget-main-content').style;
    elStyle.padding = "5px";
    
    /*style the annoration comments container
    var elStyle = $('annoration-widget-comments').style;
    elStyle.borderTop = "solid 1px #2e3192";*/
    
    //style the annoration summaries
    var els = document.getElementsByClassName('annoration-widget-summaries');
    for (i = 0; i < els.length; i++)
    {
      els[i].style.marginTop = "5px";
    }
    
    //make sure all checkboxes are checked.
    els = document.getElementsByTagName("input");
    for (i = 0; i < els.length; i++)
    {
      if (els[i].type == "checkbox")
        els[i].checked = true;
    }
    
    document.getElementById('annoration-widget-require-login').checked = false;
    document.getElementById('custom-url').checked = false;
    document.getElementById('annoration-widget-average-rating-text').value = "Average Rating:";   
    document.getElementById('annoration-widget-star-over-color-green').checked = true;
    document.getElementById('build-widget').value = "Build Widget";
  }
  
  function showCustomUrl()
  {
    
    if (document.getElementById('custom-url').checked)
    {
      document.getElementById('annoration-widget-custom-url-container').style.display = "block";
    }
    else
    {
      document.getElementById('annoration-widget-custom-url-container').style.display = "none";
    }
  }
  
  function showHeader()
  {
    if (document.getElementById('show-header').checked)
    {
      document.getElementById('annoration-widget-header').style.display = "block";
    }
    else
    {
      document.getElementById('annoration-widget-header').style.display = "none";
    }  
  }
  
  function showStars()
  {
    if (document.getElementById('show-stars').checked)
    {
      document.getElementById('annoration-widget-stars').style.display = "block";
      document.getElementById('annoration-widget-star-over-colors').style.display = "block";
    }
    else
    {
      document.getElementById('annoration-widget-stars').style.display = "none";
      document.getElementById('annoration-widget-star-over-colors').style.display = "none";
    }  
  }
  
  function showAverageRating()
  {
    if (document.getElementById('average-rating').checked)
    {
      document.getElementById('annoration-widget-average-rating').style.display = "block";
      document.getElementById('annoration-widget-average-rating-text-container').style.display = "inline";
    }
    else
    {
      document.getElementById('annoration-widget-average-rating').style.display = "none";
      document.getElementById('annoration-widget-average-rating-text-container').style.display = "none";
    }  
  }
  
  function showNumberRatings()
  {
    if (document.getElementById('number_ratings').checked)
    {
      document.getElementById('annoration-widget-total-ratings').style.display = "block";
    }
    else
    {
      document.getElementById('annoration-widget-total-ratings').style.display = "none";
    }  
  }
  
  function showBorder()
  {
    if (document.getElementById('show-border').checked)
    {
      document.getElementById('annoration-widget-main-container').style.border = "solid 1px #2E3192";
    }
    else
    {
      document.getElementById('annoration-widget-main-container').style.border = "none";
    }
  }
  
  function showSummaries()
  {
    if (document.getElementById('show-summaries').checked)
    {
      document.getElementById('annoration-widget-comments').style.display = "block";
      document.getElementById('annoration-summary-number-container').style.display = "block";
    }
    else
    {
      document.getElementById('annoration-widget-comments').style.display = "none";
      document.getElementById('annoration-summary-number-container').style.display = "none";
    }
  }

  function updateAverageMessage()
  {
    document.getElementById('annoration-widget-average-rating-label').innerHTML = document.getElementById('annoration-widget-average-rating-text').value
  }
  
  function setStarOverColor()
  {
    var els = document.getElementsByName('annoration-widget-star-over-color');
    for(i = 0; i<els.length; i++)
    {
      if (els[i].checked)
      {
        switch(els[i].value)
        {
          case "blue":
            overColor = "blue_";
            break;
          case "purple":
            overColor = "purple_";
            break;
          case "red":
            overColor = "red_";
            break;
          default:
            overColor = "";
        }
      }
    }        
  }

  function buildCss()
  {
    var styleData = '<style type="text/css">\n';
    
    //set the style information for the main container.
    if (document.getElementById('show-border').checked)
    {
      styleData += '\t#annoration-widget-main-container {border: solid 1px #2E3192; float:left; width: 200px; background-color: #fff; }\n';
    } 
    else
    {
      styleData += '\t#annoration-widget-main-container { border: none;float:right; width: 200px; background-color: #fff; }\n';
    }
    
    //set the style information for the header if it is being shown.
    if (document.getElementById('show-header').checked)
    {
      styleData += '\t#annoration-widget-header {background-color: #2E3192; color: #fff; margin-top: 0; text-align: center; padding: 5px 0px; font-size: 110%}\n';
    }
    
    //write tags for styling information for rating summary
    if (document.getElementById('average-rating').checked)
    {
      styleData += "\t#annoration-widget-average-rating {\/*insert styling here*\/}\n\t#annoration-widget-average-rating-label {\/*insert styling here*\/}\n";
    }
    
    //no styling options for the 5 stars or they could really screw it up.
    
    //write styling tags for the annoration-widget-comments if they are chosen
    if (document.getElementById('show-summaries').checked)
    {
      styleData += "\t#annoration-widget-comments {\/*insert styling here*\/}\n\t.annoration-widget-comment-user {\/*insert styling here*\/}\n";
    }
     
    styleData+= '\t#annoration-widget-no-ratings {\/*insert no ratings found styling here*\/}\n</style>';
    document.getElementById('annoration-styles').value = styleData;
  }
  
  function buildHTML()
  {
    var singleQuote = "\u0027";
    var htmlData = '<div id="annoration-widget">\n\t<div style="display: none;" id="rating"></div>\n\t<script src="http:\/\/www.annorate.com\/javascripts\/widget_dlg.js" ><\/script>\n\t<script type="text/javascript">\n\t';
    
    //write the uri information
    if (document.getElementById('custom-url').checked && document.getElementById('annoration-widget-custom-url').text != "")
    {
      htmlData += '\turi = "' + encodeURI(document.getElementById('annoration-widget-custom-url').value) + '";\n\t'
    }
    else
    {
      htmlData += '\turi = encodeURI(location.href);\n\t';
    }
    
    htmlData += '\toverColor = "' + overColor + '";\n\t';
    htmlData += '\tdocument.write(\'<script src="http://www.annorate.com/widget_annotations?u=\' + uri + \'&';
    
    //parameter to pass to show the header if requested
    if (document.getElementById('show-header').checked)
    {
      htmlData += 'showHeader=true&';
    }        
    
    //parameter to pass to show average rating
    if (document.getElementById('average-rating').checked)
    {
      htmlData += 'showAverageRatings=true&averageRatingTitle=' + encodeURI(document.getElementById('annoration-widget-average-rating-text').value) + '&';
    }
    
    //parameter to pass to show stars
    if (document.getElementById('show-stars').checked)
    {
      htmlData += 'showStars=true&';
    }
    
    //parameter to pass to show the total number of ratings
    if (document.getElementById('number_ratings').checked)
    {
      htmlData += 'showNumberRatings=true&';
    }  
    
    //parameter to pass to show annoration user summaries
    if (document.getElementById('show-summaries').checked)
    {
      htmlData += 'numberTopRatings=' + document.getElementById('annoration-summary-number').value + '&';
    }
    
    //now add the advanced options
    if (document.getElementById('annoration-widget-require-login').checked)
    {
      htmlData += 'requireLogin=true&';
    }
    
    htmlData += '"><\\\/script>\')\n\t<\/script>\n\t<script type="text\/javascript">\n\t\tif (document.getElementById(\'stars\') != null) showRating(nSepcifiedRating);\n\t<\/script>\n<\/div>';
    document.getElementById('annoration-html').value = htmlData;
  }
  
  function validateSelections()
  {
    
    //make sure they have at least one 
    var els = document.getElementsByName('required-options')
    var enoughChecked = false;
    for (i = 0; i < els.length; i++)
    {
      if (els[i].checked)
      {
        enoughChecked = true;
      }
    }
    
    if (!enoughChecked)
    {
      alert("You must select at least one option to show annorate data (eg - show stars, include average rating, total ratings or summaries)");
      return false;
    }
    
    //check to see if they user has selected a valid number of summaries
    if (document.getElementById('show-summaries').checked)
    {
      
      numSummaries = document.getElementById('annoration-summary-number').value != "" ? parseInt(document.getElementById('annoration-summary-number').value) : 0;
      if (isNaN(numSummaries) || numSummaries < 1 || numSummaries > 5)
      {
        alert('You must select between 1 and 5 summaries to show!');
        document.getElementById('annoration-summary-number').focus();
        return false;
      }
    }
    
    if (document.getElementById('custom-url').checked)
    {
      //check to make sure the url starts with http:// and is present
      if (document.getElementById('annoration-widget-custom-url').value == "")
      {
        alert("You must provide a custom url when the box is checked!");
        return false;
      }
      
      if (document.getElementById('annoration-widget-custom-url').length <= 7)
      {
        alert("You must enter a valid url in the custom URL field!");
        document.getElementById('annoration-widget-custom-url').focus();
        return false;
      }
      else
      {
        if (document.getElementById('annoration-widget-custom-url').value.indexOf("http://") == -1 && document.getElementById('annoration-widget-custom-url').value.indexOf("https://") == -1)
        {
          alert('You must enter a valid url that starts with "http://" in the custom URL field!');
          document.getElementById('annoration-widget-custom-url').focus();          
          return false;
        }
      }
    }    
    
    return true;
  }

  function buildWidgetSnippet()
  {
    //check to make sure they will get something back from their widget and that
    //if the custom url box is checked they have supplied a URL.
    if (validateSelections())
    {
      document.getElementById('build-widget').value = "Update Widget";
      buildCss();
      buildHTML();
      document.getElementById('html-snippet').style.display = "block";
    }
    else
    {
      document.getElementById('html-snippet').style.display = "none";
    }
  }
  
  resetPageStyles();
  overColor = "";
  leaveStars();
